/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { getScanList, getSendList } from '@/services/allocation';
import { isLegalData } from '@/utils/utils';
import { message, Modal } from 'antd';

export default {
  namespace: 'dataStatistics',

  state: {
    scanList: {
      list: [],
      pagination: {},
    },
    sendList: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    // 自定义扫描，获取扫描记录列表
    *getScanList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const { n_export } = payload;
      const response = yield call(getScanList, payload);

      if (!response) return;
      const { code, data = [], msg } = response;

      if (code == 0 && data) {
        if (n_export) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              scanList: {
                list: isLegalData(data, []),
                pagination: false,
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        if (n_export) {
          message.success('导出失败');
        }
        __dva_reject();
        message.error(msg);
      }
    },
    // 自定义扫描，获取派件记录列表
    *getSendList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const { n_export } = payload;
      const response = yield call(getSendList, payload);

      if (!response) return;
      const { code, data = [], msg } = response;

      if (code == 0 && data) {
        if (n_export) {
          Modal.info({
            title: '温馨提示',
            content: '导出数据申请已提交，请至【报表下载】处下载',
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              sendList: {
                list: isLegalData(data, []),
                pagination: false,
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        if (n_export) {
          message.success('导出失败');
        }
        __dva_reject();
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
